package com.jhgsys.internal.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.system.entity.Post;
import com.jhgsys.internal.system.service.IPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.Map;

/**
 * @Author wanghw
 * @CreateTime 2024/7/26 11:39
 * @Description 岗位管理
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("post")
public class PostController  extends BaseController {
    @Autowired
    private IPostService postService;

    /**
     * 分页列表查询
     * @param post
     * @param request
     * @return
     * @throws FebsException
     */
    @GetMapping("list")
    @RequiresPermissions(value={"post:view"},logical= Logical.OR)
    public FebsResponse tenantList(Post post, QueryRequest request) throws FebsException {
        try {
            if(!hasHeadCompanyRole()){
                post.setTenantId(getCurrentTenantId());
            }
            IPage<Post> pageData = this.postService.findPosts(post, request);
            Map<String, Object> dataTable = getDataTable(pageData);
            return new FebsResponse().success().data(dataTable);
        } catch (Exception e) {
            String message = "查询岗位列表失败";
            log.error(message, e);
            throw new FebsException(message);
        }

    }

    /**
     * 新增岗位
     * @param post 岗位信息
     * @param request 查询参数
     * @return
     * @throws FebsException
     */
    @PostMapping("add")
    @RequiresPermissions(value={"post:add"},logical= Logical.OR)
    public FebsResponse add(Post post, QueryRequest request) throws FebsException {
        try {
            if(StringUtils.isBlank(post.getPostCode())){
                return new FebsResponse().fail().message("岗位编号不能为空！");
            }
            if(StringUtils.isBlank(post.getPostName())){
                return new FebsResponse().fail().message("岗位名称不能为空！");
            }
            if(StringUtils.isBlank(post.getTenantId())){
                if(hasHeadCompanyRole()){
                    return new FebsResponse().fail().message("所属公司不能为空！");
                }else {
                    post.setTenantId(getCurrentTenantId());
                }

            }
            post.setCreatedTime(new Date());
            post.setCreatedBy(getCurrentUser().getUserName());

            Post queryPost = this.postService.getOne(new LambdaQueryWrapper<Post>()
                    .eq(Post::getTenantId,post.getTenantId())
                    .and(item->item.eq(Post::getPostCode,post.getPostCode()).or()
                            .eq(Post::getPostName,post.getPostName()))
                    );
            if(ObjectUtils.isNotEmpty(queryPost)){
                return new FebsResponse().fail().message("岗位编号或岗位名称已存在！");
            }
            this.postService.createPost(post);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增岗位失败";
            log.error(message, e);
            throw new FebsException(message);
        }

    }


    /**
     * 修改岗位
     * @param post
     * @return
     * @throws FebsException
     */
    @Log("修改岗位")
    @PostMapping("update")
    @RequiresPermissions(value={"post:update"},logical= Logical.OR)
    public FebsResponse updatePost(Post post) throws FebsException {
        try {
            if(StringUtils.isBlank(post.getPostId())){
                return new FebsResponse().fail().message("岗位Id不能为空！");
            }
            if(StringUtils.isBlank(post.getPostCode())){
                return new FebsResponse().fail().message("岗位编号不能为空！");
            }
            if(StringUtils.isBlank(post.getPostName())){
                return new FebsResponse().fail().message("岗位名称不能为空！");
            }
            if(StringUtils.isBlank(post.getTenantId())){
                return new FebsResponse().fail().message("所属公司不能为空！");
            }
            post.setUpdatedBy(getCurrentUser().getUserName());
            post.setUpdatedTime(new Date());
            Post queryPost = this.postService.getOne(new LambdaQueryWrapper<Post>()
                    .eq(Post::getTenantId,post.getTenantId())
                    .and(item->item.eq(Post::getPostCode,post.getPostCode()).or()
                            .eq(Post::getPostName,post.getPostName()))
            );
            if(ObjectUtils.isNotEmpty(queryPost) && !post.getPostId().equals(queryPost.getPostId())){
                return new FebsResponse().fail().message("岗位编号或岗位名称已存在！");
            }

            this.postService.updatePost(post);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改岗位失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除岗位
     * @param postIds
     * @return
     * @throws FebsException
     */
    @Log("删除岗位")
    @GetMapping("delete/{postIds}")
    @RequiresPermissions("post:delete")
    public FebsResponse deleteRoles(@NotBlank(message = "{required}") @PathVariable String postIds) throws FebsException {
        try {
            Post post = new Post();
            post.setDeletedBy(getCurrentUser().getUserName());
            post.setDeletedTime(new Date());
            post.setDeletedFlg(FebsConstant.INVALID);
            this.postService.updatePostDeletedFlg(postIds,post);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除岗位失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


    /**
     * 修改岗位状态
     * @param post
     * @return
     * @throws FebsException
     */
    @Log("修改岗位状态")
    @PostMapping("updatePostStatus")
    @RequiresPermissions(value={"post:update"},logical= Logical.OR)
    public FebsResponse updatePostStatus(Post post) throws FebsException {
        try {
            if(StringUtils.isBlank(post.getPostId())){
                return new FebsResponse().fail().message("岗位Id不能为空！");
            }
            if(StringUtils.isBlank(post.getStatus())){
                return new FebsResponse().fail().message("岗位状态不能为空！");
            }
            post.setUpdatedBy(getCurrentUser().getUserName());
            post.setUpdatedTime(new Date());

            this.postService.updateById(post);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改岗位状态失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
